var Assignee = function () {

    this.$grid_selector = $("#jira_assignee_grid_table");
    this.pager_selector = "#jira_assignee_grid_pager";
    this.$jiraExcelExportForm = $('#jira_assignee_excel_export_form');

    // 最后搜索内容
    this.lastSearch = {assignee: '', startDate: '', endDate: ''};

    this.colModel = [
        {name: 'issueid', hidden: true, width: 20, key: true},
        {name: 'authorName', width: 20},
        {name: 'author', width: 20},
        {
            name: 'issuekey', width: 20, formatter: function (cell) {
                return '<a target="_blank" href="' + _.template($.Const.JIRA_KEYWORD_URL)({keyword: cell}) + '">' + cell + '</a>'
            }
        },
        {name: 'summary', width: 20},
        {
            name: 'createdStr', width: 30
        }
    ];
    this.options = {
        moduleKey: 'jira/assignee',
        datatype: 'local',
        pager: this.pager_selector,
        sortname: 'createdStr',
        caption: "列表",
        colNames: ['ID', '姓名', 'JIRA账号', 'IssueKey', "标题", "创建日期"],
        colModel: this.colModel,
        navbarDefaultIcon: {
            add: false,
            edit: false,
            view: false,
            del: false,
            refresh: false
        }
    };
};

Assignee.prototype = {
    // 初始化表格
    initGird: function () {

        var self = this;
        this.$grid_selector
            .xumf_jqGird(this.options)
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "导出",
                buttonicon: "fa fa-download",
                onClickButton: function () {
                    var gridParam = self.$grid_selector.getGridParam();
                    var extend = $.extend({sord: gridParam.sortorder, sidx: gridParam.sortname}, self.lastSearch);
                    self.$jiraExcelExportForm.find('input[name="issueChangeDTO"]').val(JSON.stringify(extend));
                    self.$jiraExcelExportForm.submit();
                },
                position: "first"
            })
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "搜索",
                buttonicon: "fa fa-search orange",
                onClickButton: function () {
                    self.searchUI();
                },
                position: "first"
            })


    },
    searchUI: function () {
        var self = this;
        layer.open({
            maxmin: false,
            title: '搜索',
            area: [600, 300],
            btn: ['搜索', '取消'],
            content: _.template('<form style="padding:12px;" class="form-horizontal">\
                        <div class="form-group">\
                            <label class="col-xs-3 control-label" for="assignee">JIRA账号/姓名：</label>\
                            <div class="col-xs-9">\
                                <input name="assignee" class="form-control" value="{{assignee}}" autocomplete="off" type="text">\
                            </div>\
                        </div>\
                        <div class="form-group">\
                            <label class="col-xs-offset-1 col-xs-2 control-label" for="startDate">开始时间：</label>\
                            <div class="col-xs-3">\
                                <input name="startDate" data-date-format="yyyy-mm-dd" class="form-control date-picker" value="{{startDate}}" autocomplete="off" type="text">\
                            </div>\
                            <label class="col-xs-offset-1 col-xs-2 control-label" for="endDate">结束时间：</label>\
                            <div class="col-xs-3">\
                                <input name="endDate" data-date-format="yyyy-mm-dd" class="form-control date-picker" value="{{endDate}}" autocomplete="off" type="text">\
                            </div>\
                        </div>\
                    </form>')(this.lastSearch),
            yes: function (index, layero) {
                var $form = $(layero).find('form');
                self.lastSearch = {
                    assignee: $form.find('input[name=assignee]').val(),
                    startDate: $form.find('input[name=startDate]').val(),
                    endDate: $form.find('input[name=endDate]').val()
                };
                self.$grid_selector.setGridParam({
                    datatype: "json",
                    postData: {searchString: JSON.stringify(self.lastSearch)}
                }).trigger('reloadGrid');
                layer.close(index);
            },
            success: function (layero) {
                $(layero).find('.date-picker').datepicker({
                    autoclose: true,
                    todayHighlight: true
                });
            }
        })
    }
};

